package com.sunny.ynhz.utils;

import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by luao on 2017/8/14.
 */

public class DeviceMsgUtils {

    public static  String getIMEI(Context context) {

        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //  大于等于21即为5.0及以上执行内容
            Method method = null;
            try {
                method = manager.getClass().getMethod("getDeviceId", int.class);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }

            String imei1 = "";

            try {
                imei1 = (String) method.invoke(manager, 0);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            String imei2 = "";
            try {
                imei2 = (String) method.invoke(manager, 1);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            return imei1 + imei2;
        } else {
            //  低于21即为5.0以下执行内容
            return manager.getDeviceId();
        }


    }
}
